<template>
  <div class="main">
    <!-- 顶部轮播 -->
    <swiper :options="swiperOption">
      <swiper-slide v-for="(item, index) in bannerData" :key="index">
        <a target="_blank" :href="item.url">
          <img :src="item.image" alt="" srcset="" />
        </a>
      </swiper-slide>
    </swiper>
    <div class="center">
      <!-- 分类 -->
      <div class="classify">
        <el-radio-group
          class="row"
          v-for="(item, index) in classify"
          :key="index"
          v-model="item.name"
          @change="
            (val) => {
              change(item.key, val);
            }
          "
        >
          <el-radio-button
            class="item"
            v-for="(items, index) in item.list"
            :key="index"
            :label="items"
          ></el-radio-button>
        </el-radio-group>
      </div>
      <!-- 歌手、专辑、单曲 -->
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="歌手" name="first">
          <div class="singer">
            <div class="body">
              <router-link
                :to="{ name: 'singer', query: { id: item.id } }"
                v-for="(item, index) in musicSingerDataList"
                :key="index"
              >
                <el-image lazy :src="item.avatar" alt="" srcset="" />
                <h1>{{ item.name }}</h1>
                <p>{{ item.music_count }}首歌曲</p>
              </router-link>
            </div>
          </div>
          <!-- 加载更多 -->
          <div class="more-btn" @click="loadMore('歌手')">加载更多</div>
        </el-tab-pane>
        <el-tab-pane label="专辑" name="second">
          <div class="album">
            <div class="body">
              <router-link
                :to="{ name: 'album', query: { id: item.id } }"
                v-for="(item, index) in musicCollectionListData"
                :key="index"
              >
                <div class="img">
                  <el-image lazy :src="item.image" alt="" srcset="" />
                  <el-image
                    lazy
                    class="icon"
                    :src="require('../assets/images/qq.png')"
                    alt=""
                  />
                </div>
                <h1 v-html="item.name"></h1>
                <p>{{ item.show_time }}</p>
              </router-link>
            </div>
          </div>
          <!-- 加载更多 -->
          <div class="more-btn" @click="loadMore('专辑')">加载更多</div>
        </el-tab-pane>
        <el-tab-pane label="单曲" name="third">
          <div class="single">
            <div
              class="item"
              v-for="(item, index) in musicListData"
              :key="index"
            >
              <div class="column">
                <div class="title">歌曲:</div>
                <div class="cont">{{ item.name }}</div>
                <div class="tag" v-if="item.mv != null">MV</div>
              </div>
              <div class="column">
                <div class="title">歌手:</div>
                <div class="cont">{{ item.singer }}</div>
              </div>
              <div class="column">
                <div class="title">专辑:</div>
                <div class="cont">{{ item.collection }}</div>
              </div>
              <div class="column">
                <div class="title">时长:</div>
                <div class="cont">{{ item.duration }}</div>
              </div>
              <div class="column">
                <div class="title">歌词:</div>
                <router-link
                  :to="{ name: 'single', query: { id: item.id } }"
                  class="view-btn"
                  >查看</router-link
                >
              </div>
              <div class="column">
                <a
                  v-if="
                    item.url_1 != null &&
                    item.url_1 != '' &&
                    item.url_1 != 'None' &&
                    item.url_1 != undefined
                  "
                  class="img"
                  :href="item.url_1"
                  target="_blank"
                  rel="noopener noreferrer"
                >
                  <el-image
                    lazy
                    :src="require('../assets/images/qq.png')"
                    alt=""
                    srcset=""
                /></a>
                <a
                  v-if="
                    item.url_2 != null &&
                    item.url_2 != '' &&
                    item.url_2 != 'None' &&
                    item.url_2 != undefined
                  "
                  class="img"
                  :href="item.url_2"
                  target="_blank"
                  rel="noopener noreferrer"
                >
                  <el-image
                    lazy
                    :src="require('../assets/images/kg.png')"
                    alt=""
                    srcset=""
                /></a>
                <a
                  v-if="
                    item.url_3 != null &&
                    item.url_3 != '' &&
                    item.url_3 != 'None' &&
                    item.url_3 != undefined
                  "
                  class="img"
                  :href="item.url_3"
                  target="_blank"
                  rel="noopener noreferrer"
                >
                  <el-image
                    lazy
                    :src="require('../assets/images/wyy.png')"
                    alt=""
                    srcset=""
                /></a>
                <a
                  v-if="
                    item.url_4 != null &&
                    item.url_4 != '' &&
                    item.url_4 != 'None' &&
                    item.url_4 != undefined
                  "
                  class="img"
                  :href="item.url_4"
                  target="_blank"
                  rel="noopener noreferrer"
                >
                  <el-image
                    lazy
                    :src="require('../assets/images/kw.png')"
                    alt=""
                    srcset=""
                /></a>
              </div>
            </div>
          </div>
          <!-- 加载更多 -->
          <div class="more-btn" @click="loadMore('单曲')">加载更多</div>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!-- 广告 -->
    <div class="ad">
      <a
        target="_blank"
        class="item"
        v-for="(item, index) in adData.slice(0, 5)"
        :key="index"
        :href="item.url"
      >
        <el-image lazy :src="item.image_mini" alt="" />
      </a>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 顶部轮播图设置
      swiperOption: {
        autoplay: {
          disableOnInteraction: false, // 用户操作swiper之后，是否禁止autoplay
          delay: 5000, // 自动切换的时间间隔（单位ms）
        },
        // loop: true,
      },
      adData: [],
      bannerData: [],
      //   分类
      classify: [],
      activeName: "first", //tab切换
      musicSingerDataList: [], //歌手列表
      musicCollectionListData: [], //专辑列表
      musicListData: [], //歌曲列表
      //请求参数
      requestData: {
        keyword: "",
        page: 1,
      },
    };
  },
  props: ["_keyword"], //接收父组件传值
  // 监听父组件传的_keyword值的变化
  watch: {
    _keyword: function (_keyword) {
      this.requestData.keyword = _keyword;
      this.requestData.page = 1;
      if (this.activeName === "first") {
        this.musicSingerDataList = [];
        this.requestData.limit = 24;
        this.getMusicSingerList(this.requestData);
      } else if (this.activeName === "second") {
        this.musicCollectionListData = [];
        this.requestData.limit = 24;
        this.getMusicCollectionList(this.requestData);
      } else if (this.activeName === "third") {
        this.musicListData = [];
        this.requestData.limit = 30;
        this.getMusicList(this.requestData);
      }
      scrollTo(0, 0);
    },
  },
  created() {
    // 广告
    this.$http.getAd().then((res) => {
      if (res.data.code === 0) {
        this.adData = res.data.data;
      } else {
        this.$message.error("获取失败，请重试！");
      }
    });
    // 顶部轮播
    this.$http.getBanner().then((res) => {
      if (res.data.code === 0) {
        this.bannerData = res.data.data;
      } else {
        this.$message.error("获取失败，请重试！");
      }
    });
    this.requestData.page = 1;
    this.requestData.keyword = "";
    this.requestData.limit = 24;
    this.getMusicSingerList(this.requestData);
    // 分类获取
    this.$http.getSearch({ type: 4 }).then((res) => {
      if (res.data.code === 0) {
        res.data.data.map((item) => {
          item.name = "全部";
          item.list.unshift("全部");
        });
        this.requestData.page = 1;
        this.requestData.keyword = "";
        this.classify = res.data.data;
      } else {
        this.$message.error("获取失败，请重试！");
      }
    });
  },
  methods: {
    // 分类切换
    change(key, val) {
      this.requestData.page = 1;
      this.requestData[key] = val;
      this.movieData = [];
      if (val === "全部") {
        this.requestData[key] = "";
        if (this.activeName === "first") {
          this.musicSingerDataList = [];
          this.requestData.limit = 24;
          this.getMusicSingerList(this.requestData);
        } else if (this.activeName === "second") {
          this.musicCollectionListData = [];
          this.requestData.limit = 24;
          this.getMusicCollectionList(this.requestData);
        } else if (this.activeName === "third") {
          this.musicListData = [];
          this.requestData.limit = 30;
          this.getMusicList(this.requestData);
        }
      } else {
        if (this.activeName === "first") {
          this.musicSingerDataList = [];
          this.requestData.limit = 24;
          this.getMusicSingerList(this.requestData);
        } else if (this.activeName === "second") {
          this.musicCollectionListData = [];
          this.requestData.limit = 24;
          this.getMusicCollectionList(this.requestData);
        } else if (this.activeName === "third") {
          this.musicListData = [];
          this.requestData.limit = 30;
          this.getMusicList(this.requestData);
        }
      }
    },
    //加载更多
    loadMore(e) {
      if (e === "歌手") {
        this.requestData.limit = 24;
        ++this.requestData.page;
        this.getMusicSingerList(this.requestData);
      } else if (e === "专辑") {
        this.requestData.limit = 24;
        ++this.requestData.page;
        this.getMusicCollectionList(this.requestData);
      } else if (e === "单曲") {
        this.requestData.limit = 30;
        ++this.requestData.page;
        this.getMusicList(this.requestData);
      }
    },
    //切换
    handleClick(tab, event) {
      // console.log(tab, event);
      // console.log(this.activeName);
      this.requestData.page = 1;
      if (this.activeName === "first") {
        this.musicSingerDataList = [];
        this.requestData.limit = 24;
        this.getMusicSingerList(this.requestData);
      } else if (this.activeName === "second") {
        this.musicCollectionListData = [];
        this.requestData.limit = 24;
        this.getMusicCollectionList(this.requestData);
      } else if (this.activeName === "third") {
        this.musicListData = [];
        this.requestData.limit = 30;
        this.getMusicList(this.requestData);
      }
    },
    // 歌手列表
    getMusicSingerList(data) {
      this.$http.getMusicSingerList(data).then((res) => {
        if (res.data.code === 0) {
          if (res.data.data.length <= 0) {
            this.$message.error("没有更多数据！");
            return;
          }
          this.musicSingerDataList = this.musicSingerDataList.concat(
            res.data.data
          );
        } else {
          this.$message.error("获取失败，请重试！");
        }
      });
    },
    // 专辑列表
    getMusicCollectionList(data) {
      this.$http.getMusicCollectionList(data).then((res) => {
        if (res.data.code === 0) {
          if (res.data.data.length <= 0) {
            this.$message.error("没有更多数据！");
            return;
          }
          this.musicCollectionListData = this.musicCollectionListData.concat(
            res.data.data
          );
        } else {
          this.$message.error("获取失败，请重试！");
        }
      });
    },
    // 歌曲列表
    getMusicList(data) {
      this.$http.getMusicList(data).then((res) => {
        if (res.data.code === 0) {
          if (res.data.data.length <= 0) {
            this.$message.error("没有更多数据！");
            return;
          }
          this.musicListData = this.musicListData.concat(res.data.data);
        } else {
          this.$message.error("获取失败，请重试！");
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep {
  .swiper-container {
    height: 400px;
    width: 100%;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .el-radio-button__inner {
    border: none;
    font-size: 20px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #333333;
    padding: 9px;
    border-radius: 4px;
    background: none;
  }
  .el-radio-button:first-child .el-radio-button__inner {
    border: none;
  }
  .el-radio-button:first-child .el-radio-button__inner,
  .el-radio-button:last-child .el-radio-button__inner {
    border-radius: 4px;
  }
  .el-radio-button__orig-radio:checked + .el-radio-button__inner {
    background: #e5e5e5;
    color: #ff8000;
    box-shadow: none;
    -webkit-box-shadow: none;
    border: none;
  }
  .el-tabs {
    margin-top: 80px;
  }
  .el-tabs__item {
    font-size: 16px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #333333;
  }
  .el-tabs__item.is-active {
    font-size: 18px;
    font-weight: bold;
  }
  .el-tabs__nav-wrap::after {
    display: none;
  }
  .el-tabs__active-bar {
    height: 4px;
    background: #333333;
    border-radius: 20px;
  }
}
.center {
  width: 1600px;
  margin: 0 auto;
  .classify {
    .row {
      display: flex;
      margin-top: 20px;
      .item {
        font-size: 20px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #333333;
        padding: 6px 9px;
        border-radius: 4px;
        cursor: pointer;
      }
      .item.active {
        background: #e5e5e5;
        color: #ff8000;
      }
    }
  }

  .singer {
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 45px;
      .lf {
        font-size: 20px;
        font-family: PingFang SC;
        font-weight: bold;
        color: #333333;
      }
      .rt {
        height: 45px;
        display: flex;
        align-items: center;
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: 400;
        line-height: 40px;
        color: #666666;
        .el-image {
          width: 14.85px;
          height: 25.46px;
          margin-left: 10px;
        }
      }
    }
    .body {
      display: flex;
      flex-wrap: wrap;
      a {
        display: inline-block;
        width: 220px;
        margin-top: 30px;
        .el-image {
          width: 220px;
          height: 220px;
          border-radius: 50%;
        }
        h1 {
          font-size: 20px;
          font-family: PingFang SC;
          font-weight: 400;
          line-height: 45px;
          color: #333333;
          text-align: center;
          margin: 20px 0;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        p {
          font-size: 16px;
          font-family: PingFang SC;
          font-weight: 400;
          line-height: 22px;
          color: #999999;
          text-align: center;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
      a:not(:nth-child(6n)) {
        margin-right: 56px;
      }
    }
  }
  .album {
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 45px;
      .lf {
        font-size: 20px;
        font-family: PingFang SC;
        font-weight: bold;
        color: #333333;
      }
      .rt {
        height: 45px;
        display: flex;
        align-items: center;
        font-size: 16px;
        font-family: PingFang SC;
        font-weight: 400;
        line-height: 40px;
        color: #666666;
        .el-image {
          width: 14.85px;
          height: 25.46px;
          margin-left: 10px;
        }
      }
    }
    .body {
      display: flex;
      flex-wrap: wrap;
      a {
        display: inline-block;
        width: 246px;
        margin-top: 30px;
        .img {
          width: 246px;
          height: 164px;
          border-radius: 10px;
          overflow: hidden;
          position: relative;
          .el-image {
            width: 100%;
            height: 100%;
            border-radius: 10px;
          }
          .icon {
            width: 28px;
            height: 28px;
            position: absolute;
            bottom: 5px;
            right: 5px;
          }
        }
        h1 {
          font-size: 20px;
          font-family: PingFang SC;
          font-weight: 400;
          color: #333333;
          text-align: right;
          margin: 10px 0;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        p {
          font-size: 16px;
          font-family: PingFang SC;
          font-weight: 400;
          line-height: 22px;
          color: #999999;
          text-align: right;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
      a:not(:nth-child(6n)) {
        margin-right: 24px;
      }
    }
  }
  .single {
    margin-top: 30px;
    .item {
      display: flex;
      margin-bottom: 20px;
      .column {
        font-size: 18px;
        font-family: PingFang SC;
        font-weight: 400;
        display: flex;
        align-items: center;
        height: 40px;
        .title {
          color: #999999;
          width: 54px;
        }
        .cont {
          color: #333333;
          width: 180px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
        .tag {
          width: 40px;
          height: 20px;
          line-height: 20px;
          border: 1px solid #ff8000;
          border-radius: 2px;
          color: #ff8000;
          font-size: 14px;
          font-weight: 400;
          text-align: center;
          position: relative;
          top: -6px;
          left: 5px;
        }
        .view-btn {
          line-height: 20px;
          padding: 2px 8px;
          border: 1px solid #666666;
          border-radius: 4px;
          text-align: center;
          font-size: 14px;
          font-family: PingFang SC;
          font-weight: 400;
          color: #666666;
          cursor: pointer;
        }
        .img {
          cursor: pointer;
          width: 38px;
          height: 38px;
          .el-image {
            width: 100%;
            height: 100%;
          }
        }
        .img:not(:last-child) {
          margin-right: 30px;
        }
      }
      .column:first-child {
        align-items: flex-end;
      }
      .column:not(:last-child) {
        margin-right: 30px;
      }
    }
  }

  .more-btn {
    width: 240px;
    height: 60px;
    background: #ffffff;
    border: 1px solid #707070;
    border-radius: 4px;
    font-size: 20px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 60px;
    color: #333333;
    text-align: center;
    margin: 0 auto;
    margin-top: 40px;
    margin-bottom: 80px;
    cursor: pointer;
  }
}
.ad {
  position: fixed;
  top: 150px;
  right: 100px;
  z-index: 1000;
  .item {
    display: block;
    width: 150px;
    height: 150px;
    border-radius: 15px;
    overflow: hidden;
    .el-image {
      width: 100%;
      height: 100%;
    }
  }
  .item:not(:first-child) {
    margin-top: 30px;
  }
}
</style>